package com.iris.live.services.data.repositories;

import com.iris.live.services.data.models.顾客试驾记录Model;
import com.iris.live.services.data.models.顾客试驾记录ModelPK;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by hyp on 2016/12/6.
 */
@Transactional
public interface 顾客试驾记录Repository extends JpaRepository<顾客试驾记录Model, 顾客试驾记录ModelPK>, JpaSpecificationExecutor<顾客试驾记录Model> {

    @Modifying
    @Query("select a from 顾客试驾记录Model a where a.进店编号=?1")
    List<顾客试驾记录Model> findBy进店编号(Integer id);

    @Modifying
    @Transactional
    @Query(value = "UPDATE 顾客试驾记录 a , 顾客进店记录表 b set a.试驾结束时间 = b.离店时间 WHERE a.进店编号 = b.进店编号 " +
            " and a.试驾结束时间 IS NULL and b.离店时间 is not null and 进店日期 = DATE_SUB(CURDATE(),INTERVAL 1 day)",nativeQuery = true)
    int updateDriveTimeEnd();

    @Modifying
    @Transactional
    @Query(value = "UPDATE 顾客试驾记录 a , 顾客进店记录表 b SET a.试驾结束时间 = b.到店时间 WHERE a.进店编号 = b.进店编号  and a.试驾结束时间 IS NULL " +
            "and b.离店时间 is null and b.到店时间 is not null and 进店日期 = DATE_SUB(CURDATE(),INTERVAL 1 day)",nativeQuery = true)
    int updateDriveTimeStart();
}
